package com.example.unlock

import android.os.Handler
import android.util.Log
import android.view.View
import android.widget.ImageView

class UnlockPresenter (
    private val mView: IUnlockView,
    private val mDotViewList: List<ImageView>,
    private val mImageViewModelsList: List<ImageViewModel>,
    private val mLineTagList: List<Int>
        ){
    private lateinit var user:User
    private var lastSelectedDotView:ImageView? = null
    private val passwordBuilder = StringBuilder()
    private var firstPassword = "" //记录第一次的密码
    private val seletedViewArray = arrayListOf<ImageView>() //所有点亮的视图
    fun loadUserInfo(){
        user = User("zhangsan","",false)
        if (user.isLogin){
            mView.showAlertText("请绘制密码图案")
        }else{
            mView.showAlertText("请设置密码图案")
        }
    }

    fun userTouch(x:Float, y:Float){
        // 判断触摸点是否在某个区域内部 如果没有 提前return
        val dotView = isInView(x, y) ?: return

        //判断是不是第一个触摸点
        if (lastSelectedDotView == null){
            //点亮点
            highlightDotView(dotView)
        }else{
            //判断路径是否可达
            val lineView = hasLine(dotView) ?: return
           //点亮点 线
            highlightDotView(dotView)
            highlightLineView(lineView)
        }
    }

    fun endTouch(){
        Log.v("pxd",passwordBuilder.toString())
        if (user.isLogin){
            if (user.password == passwordBuilder.toString()){
                mView.showAlertText("解锁成功")
            }else{
                mView.showAlertText("解锁失败 请重新绘制密码图案")
            }
            clear()
        }else{
            if (firstPassword.isEmpty()){
                firstPassword = passwordBuilder.toString()
                mView.showAlertText("请确认密码图案")
                lastSelectedDotView = null
                passwordBuilder.clear()
            }else{
                if (firstPassword == passwordBuilder.toString()){
                    mView.showAlertText("设置密码成功")
                    user.password = firstPassword
                    user.isLogin = true
                    mView.switchActivity()
                    lastSelectedDotView = null
                    passwordBuilder.clear()
                }else{
                    mView.showAlertText("两次密码不一致，请重新设置")
                    clear()
                }
            }
        }

        hideView()
    }

    private fun clear(){
        firstPassword = ""
        lastSelectedDotView = null
        passwordBuilder.clear()
    }

    private fun hideView(){
        Handler().postDelayed({
            seletedViewArray.forEach { it.visibility = View.INVISIBLE }
            seletedViewArray.clear()
        },500)
    }

    private fun hasLine(view: ImageView):ImageView?{
        //获取两个点的tag值
        val last = (lastSelectedDotView!!.tag as String).toInt()
        val current = (view!!.tag as String).toInt()
        //获取线条的tag
        val linetag = if (last < current) last*10+current else current*10 + last
        //判断lineTagList里面是否有这个值
        return if (mLineTagList.contains(linetag)){
            //找到对应的视图
            findLineWithTag(linetag)
        }else{
            null
        }
    }

    private fun findLineWithTag(lineTag:Int): ImageView?{
        Log.v("pxd", "findLineWithTag: $lineTag")
        mImageViewModelsList.forEach {
            if (it.view.tag == "$lineTag"){
                return it.view
            }
        }
        return null
    }

    private fun highlightDotView(view: ImageView){
        view.visibility = View.VISIBLE
        lastSelectedDotView = view //记录上一个点
        passwordBuilder.append(view.tag as String) //记录密码
        seletedViewArray.add(view)
    }
    private fun highlightLineView(view: ImageView){
        view.visibility = View.VISIBLE
        seletedViewArray.add(view)
    }

    private fun isInView(x:Float, y: Float): ImageView?{
        mDotViewList.forEach {
            if ((x >= it.x && x <= it.x+it.width) && (y >= it.y && y <= it.y + it.height)){
                return  it
            }
        }
        return null
    }
}